/*$T indentinput.c GC 1.140 02/15/09 22:54:20 */

/**$6
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 *
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */

#include <Config.h>
#include <stddef.h>
#include "osa.h"
#include "OSAcfg.h"

/* include "my_osa_init.h" */
#include "rs485.h"

/*
 * Index for byte transmission ;
 * It's initialised inside drvrs485
 */
int TX_ByteNumber;
int TX_len;
#if defined(__C30__)
void __attribute__ ((__interrupt__, auto_psv))_T1Interrupt(void)
{

	/* 100 ms tick */
	PR1 = 25000 - 1;
	OS_Timer();
	_T1IF = 0;
}	/* T1Interrupt */

void __attribute__ ((__interrupt__, auto_psv))_OscillatorFail(void)
{
	while (1);
}

void __attribute__ ((__interrupt__, auto_psv))_StackError(void)
{
	while (1);
}

void __attribute__ ((__interrupt__, auto_psv))_MathError(void)
{
	while (1);
}

/* Part of the transmiter interrupt routine. */
void __attribute__ ((__interrupt__, auto_psv))_U1TXInterrupt(void)
{
	/*~~~~~~~~~*/
	char	temp;
	char	*p;
	/*~~~~~~~~~*/

	temp = OS_DI();
	if ((TX_ByteNumber > 0) && (TX_ByteNumber < TX_len))
	{
		while (U1STAbits.UTXBF);
		U1TXREG = (unsigned char) strPacket[TX_ByteNumber];
	}

	if (TX_ByteNumber == (TX_len))
	{
		IEC0bits.U1TXIE = 0;
		TX_ByteNumber = -1;
		p = (char *) &strPacket;
		p = 0;
	}

	IFS0bits.U1TXIF = 0;
	TX_ByteNumber++;
	OS_RI(temp);
	return;
}

/* Part of the receiver interrupt routine. */
void __attribute__ ((__interrupt__, auto_psv))_U1RXInterrupt(void)
{
	/*~~~~~~~~~~*/
	static int	i;
	char	temp;
	char	a, b;
	/*~~~~~~~~~~*/

	temp = OS_DI();
	IFS0bits.U1RXIF = 0;
	if (!OS_SQueue_IsFull(DRV_rs485_q_char))
	{
		OS_SQueue_Send_I(DRV_rs485_q_char, U1RXREG);
		a = SER_ERR;

		/* Settiamo il flag SER_RX per avvisare che il buffer non è vuoto */
		b = Rs485Flag;
		i++;
		OS_Flag_Set_1(Rs485Flag, SER_RX);
	}
	else

	/*
	 * DRV_rs485_q_char is full ;
	 * Let's raise an error
	 */
	{
		OS_Flag_Set_1(Rs485Flag, (SER_RX & SER_ERR));
		Nop();
	}

	OS_RI(temp);
	return;
}



#elif defined(__18CXX)
void	rx_isr(void);
void	tx_isr(void);

/**
 =======================================================================================================================
 *
 =======================================================================================================================
 */
void rx_isr(void)
{
	/*~~~~~~~~~~*/
	static int	i;
	char	temp;
	char	a;
	char	b;
	/*~~~~~~~~~~*/

INTCONbits.GIE = 0;
//	temp = OS_DI();
	PIR1bits.RCIF = 0;
	if (!OS_SQueue_IsFull(DRV_rs485_q_char))
	{
		OS_SQueue_Send_I(DRV_rs485_q_char, RCREG);
		a = SER_ERR;

		/* Settiamo il flag SER_RX per avvisare che il buffer non è vuoto */
		b = Rs485Flag;
		i++;
		OS_Flag_Set_1(Rs485Flag, SER_RX);
	}
	else

	/*
	 * DRV_rs485_q_char is full ;
	 * Let's raise an error
	 */
	{
		OS_Flag_Set_1(Rs485Flag, (SER_RX & SER_ERR));
		Nop();
	}

//	OS_RI(temp);
INTCONbits.GIE = 1;
	return;
}

/**
 =======================================================================================================================
 *
 =======================================================================================================================
 */
void tx_isr(void)
{
	/*~~~~~~~*/
	char	*p;
	/*~~~~~~~*/

	if ((TX_ByteNumber > 0) && (TX_ByteNumber < TX_len))
	{
		TXREG = (unsigned char) strPacket[TX_ByteNumber];
	}

	if (TX_ByteNumber == (TX_len))
	{
		PIE1bits.TXIE = 0;
		TX_ByteNumber = -1;
		p = (char *) &strPacket;
		p = 0;
	}

	TX_ByteNumber++;
	return;
}

/**
 =======================================================================================================================
 *
 =======================================================================================================================
 */
void InterruptVectorH(void)
{
	OS_EnterInt();
	if ((PIR1bits.RCIF) & (PIE1bits.RCIE)) rx_isr();
	if ((PIR1bits.TXIF) & (PIE1bits.TXIE)) tx_isr();
	INTCONbits.GIE = 1;
	OS_LeaveInt();
}



/**
 =======================================================================================================================
 *
 =======================================================================================================================
 */
void InterruptVectorL(void)
{	
//	OS_DI();
//	while(1); 
}
#pragma code IT_vector_low = 0x18
void Interrupt_low_vec (void)
{
	_asm goto InterruptVectorL _endasm
}
#pragma code
#pragma code IT_vector_high = 0x08
void Interrupt_high_vec (void)
{
	_asm goto InterruptVectorH _endasm
}
#pragma code
#endif
